package com.erastep.mvc.proxy
{
	import com.erastep.mvc.EraStepFrame;
	import com.erastep.mvc.ifc.IFrame;
	import com.erastep.mvc.ifc.INotification;
	import com.erastep.mvc.ifc.IProxy;
	import com.erastep.mvc.module.SimpleModule;
	import com.erastep.util.CommonConstants;
	import com.erastep.util.GUID;
	import com.erastep.util.LogUtil;
	import com.erastep.util.StringUtil;
	
	import mx.controls.Alert;
	import mx.messaging.ChannelSet;
	import mx.messaging.Consumer;
	import mx.messaging.channels.AMFChannel;
	import mx.messaging.events.MessageEvent;
	import mx.messaging.events.MessageFaultEvent;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.remoting.mxml.Operation;
	import mx.rpc.remoting.mxml.RemoteObject;

	/**
	 * @author Jarvis 
	 * @date 2011/4/7
	 * @depart NB
	 * */
	public class Proxy implements IProxy
	{
		
		private var _remote:RemoteObject;
		
		public function Proxy(proxyName:String = null)
		{
			this._proxyName = (proxyName != null)?proxyName:NAME; 
			iframe = EraStepFrame.getInstance();
			//_remote = new RemoteObject();
		}
		
		public function get remote():RemoteObject{
			return _remote;
		}
		
		private var _host:String;
		
		public var _mg:SimpleModule;
		
		protected var _proxyName:String;
		
		private var iframe:IFrame;
		
		/**
		 * 预设Key
		 * */
		public static const NAME:String = "simpleProxy";
		/**
		 * 消息发送方法.
		 * @param String notificationName 消息名称
		 * @param Object body 消息主体
		 * @param String type 消息类型
		 * @param String toId 消息发送到哪里
		 * @param String fromId 消息源
		 * */
        public function sendNotification(notificationName:String, body:Object=null, type:String=null, toId:String=null, fromId:String=null):void{
			if(_mg){
				_mg.sendNotification(notificationName,body,type,toId,_mg.moduleKey);
			}else{
				EraStepFrame.getInstance().sendNotification(notificationName,body,type,toId,fromId);
			}
		}
		/**
		 * 与数据通道的数据交互方法.
		 * @param notificationName 消息名称
		 * @param body 消息主体
		 * @return String GUID 
		 * */
		public function sendDataNotification(notificationName:String,body:Object = null):String{
			var uid:String = GUID.create();
			body.GUID = uid;
			sendNotification(notificationName,body,CommonConstants.Flex_CALL_FLEX_TYPE,CommonConstants.VICTOPFRAME);
			return uid;
		}
		
		/**
		 * 数据获取失败后,默认接收方法.
		 * */
		public function respondToCallServerError(e:INotification):void{
        	Alert.show("请求后台失败,错误信息::"+e.getBody().statusText + " code:"+e.getBody().statusCode,"消息来自"+e.getFromId());		
		}
		
		/**
		 * 获取代理类的Key
		 * */
		public function getProxyName():String{
			return this._proxyName;
		}
		
		public function set host(host:String):void{
			this._host = host;
		}
		public function get host():String{
			return this._host;
		}

		public function sendByRemote(obj:Object):Operation{
			if(!_remote){
				_remote = new RemoteObject();
			}
			try
			{
				remote.endpoint = host+"/messagebroker/amf";
				remote.destination = obj.action;
				var ao:Operation = remote.getOperation(obj.method) as Operation;
				if(ao.hasEventListener(FaultEvent.FAULT)){
					if(obj.hasOwnProperty("fail") && obj.fail){
						ao.addEventListener(FaultEvent.FAULT,obj.fail);
					}else{
						ao.addEventListener(FaultEvent.FAULT,sendFail);
					}
				}
				if(!ao.hasEventListener(ResultEvent.RESULT)){
					ao.addEventListener(ResultEvent.RESULT,obj.success);
				}
				if(obj.hasOwnProperty("showBusy") && obj.showBusy == false){
					ao.showBusyCursor = false;
				}else{
					ao.showBusyCursor = true;
					LogUtil.print("Remote:"+remote.endpoint+"发送参数："+StringUtil.objectToJson(obj.param));
				}
				
				return ao;
			} 
			catch(error:Error) 
			{
				LogUtil.print("发送到服务端失败!\n"+error.getStackTrace());
			}
			return null;
		}
		/**
		 * 开始启动订阅消息
		 * */
		public function setStartConsumer(destination:String,subtopic:String,success:Function,fail:Function):Consumer{
			var cs:Consumer = new Consumer();
			cs.subtopic = destination+"."+subtopic;
			var channel:AMFChannel = new AMFChannel("my-polling-amf", this.host+"/messagebroker/amfpolling"); 
			var cst:ChannelSet = new ChannelSet();
			cst.addChannel(channel);
			cs.channelSet = cst;
			cs.destination = destination;
			cs.addEventListener(MessageEvent.MESSAGE,success);
			cs.addEventListener(MessageFaultEvent.FAULT,fail);
			LogUtil.print("开始启动服务器端推送服务!");
			cs.subscribe();
			return cs;
		}
		
		private function sendFail(e:FaultEvent):void{
			Alert.show("访问服务端失败!"+e.toString());
		}

		
		/**
		 * set MessageManager
		 * */
		public function set MessageManager(mg:SimpleModule):void{
			this._mg = mg;		
		}
		
		/**
		 * remove method
		 * */
		public function onRemove():void{
			
		}
		
		
		/**
		 * register method
		 * */
		public function onRegister():void{
		
		}
		
		
		
		/**
		 * 快速执行方法
		 * */
		public function callFunction(functionName:String,param:Object = null):Object{
			if(this.hasOwnProperty(functionName)){
				var fun:Function = this[functionName] as Function;
				if(fun != null){
					return fun.call(null,param);
				}else{
					Alert.show("请确认 "+functionName +" 为方法名!");
				}
			}else{
				Alert.show("此类中未找到方法名为 "+functionName);
			}
			return null;
		}
	}
}